<template>
  <div class="main-content pd16">

    <!--搜索栏-->
    <el-form class="search-form" ref="searchFormRef" :model="searchForm" :inline="true" label-width="70">

      <el-form-item label="压缩包名称" prop="originName" label-width="90">
        <el-input v-model="searchForm.zipName" placeholder="请输入" clearable style="width: 200px"
                  @keyup.enter="searchData"/>
      </el-form-item>

      <el-form-item label="创建时间" prop="time" label-width="70">
        <el-date-picker
            v-model="searchForm.time"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="searchData">搜索</el-button>
        <el-button icon="Refresh" @click="resetSearch">重置</el-button>
      </el-form-item>

    </el-form>

    <!--按钮组-->
    <!--    <div class="fx mgb12">-->
    <!--      <el-button type="danger" plain size="small" icon="Delete" @click="deleteBatch">批量删除</el-button>-->
    <!--    </div>-->

    <!--表格-->
    <el-table border highlight-current-row row-key="id" v-loading="option.loading" :data="option.tableData"
              @selection-change="selectionChange">
      <el-table-column label="压缩包名称" prop="zipName" :show-overflow-tooltip="true" align="center"
                       min-width="170"></el-table-column>
      <el-table-column label="文件大小" prop="size" :show-overflow-tooltip="true" width="200" align="center">
        <template #default="scope">
          {{ parseByteSize(scope.row.size) }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="ct" :show-overflow-tooltip="true" align="center"></el-table-column>

      <el-table-column label="生成状态" prop="createStatus" :show-overflow-tooltip="true" width="200" align="center">
        <template #default="scope">
          <el-tag v-if="scope.row.createStatus=='0'" type="warning" style="font-size: 15px">正在生成</el-tag>
          <el-tag v-if="scope.row.createStatus=='1'" type="success" style="font-size: 15px">生成完毕</el-tag>
          <el-tag v-if="scope.row.createStatus=='2'" type="danger" style="font-size: 15px">失败</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" fixed="right" width="220">
        <template #default="scope">

          <el-button v-if="scope.row.createStatus=='1'" link type="primary" icon="Download"
                     @click="downloadFile(scope.row)" style="margin-left: 10px">
            下载
          </el-button>

          <el-button v-if="scope.row.createStatus!='0' && (isJw ||isBm)" link type="primary" icon="Delete"
                     @click="submitDelete(scope.row)" style="margin-left: 10px">
            删除
          </el-button>

        </template>
      </el-table-column>

    </el-table>

    <!--分页-->
    <div class="fx_end">
      <el-pagination
          class="mgt8"
          small
          background
          :total="option.totalRow"
          :page-sizes="option.pageSizes"
          v-model:current-page="searchForm.pageNumber"
          v-model:page-size="searchForm.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="searchData"
          @current-change="searchData"
      />
    </div>

  </div>
</template>

<script setup>
import {getCurrentInstance, onMounted, ref} from "vue";
import $modal from "@/common/utils/modal";
import {copyNew, parseByteSize} from "@/common/utils/kit";
import {asDict} from "@/api/common";
import {bmDeleteZipFile, bmJsPageZip, jwDeleteZipFile, jwJsPageZip, jwTaskPageZip} from "@/api/jldz";
import {dateToStr} from "@/common/utils/date";
import {downloadChrome} from "@/common/utils/request";

const props = defineProps({
  isJw: {
    default: false
  },
  isBm: {
    default: false
  },
  isJs: {
    default: false
  },
  client: {
    default: ''
  },
})

const {proxy} = getCurrentInstance();

const {enu_room_status} = asDict("enu_room_status");

let optionKey = {
  loading: false,
  dialogType: "",
  dialogTitle: "",
  openDialog: false,
  tableData: [],
  selectIds: [],
  selectNames: [],
  pageSizes: [50, 100, 500, 1000, 2000, 5000],
  totalRow: 0,
  nowRoleId: 0,
  defaultChecked: [],
  allRole: [],
}

let searchKey = {
  pageNumber: 1,
  pageSize: 100,
  time: '',
  originName: '',
}
let formKey = {
  id: "",
  inPassword: "",//入会密码
  isBeforeIn: "",//是否允许主持人不在时进入会议
  inType: "",//入会类型
  isInMute: "",//入会后是否自动静音
  closeSettingTime: "",//会议结束时间
}
const formRules = ref({
  isBeforeIn: [{required: true, message: "不能为空", trigger: "blur"}],
  inType: [{required: true, message: "不能为空", trigger: "blur"}],
  isInMute: [{required: true, message: "不能为空", trigger: "blur"}],
  closeSettingTime: [{required: true, message: "不能为空", trigger: "blur"}],
});

const searchForm = ref(searchKey)
const formData = ref(formKey);
const option = ref(optionKey);

function resetForm() {
  formData.value = copyNew(formKey);
}

function resetSearch() {
  proxy.$refs["searchFormRef"].resetFields();
  searchData()
}

//页面加载完成回调
onMounted(() => {
  searchData()
})

function selectionChange(rows) {
  option.value.selectIds = rows.map(item => item.id);
  option.value.selectNames = rows.map(item => item.account);
}

//搜索数据
function searchData() {
  option.value.loading = true;
  searchForm.value.startTime = dateToStr(searchForm.value.time[0])
  searchForm.value.endTime = dateToStr(searchForm.value.time[1])

  // 查任务
  if (props.client == 'jwBmTask' || props.client == 'jwTeacherTask') {

    if (props.client == 'jwTeacherTask') {
      searchForm.value.taskType = "0";
    }
    if (props.client == 'jwBmTask') {
      searchForm.value.taskType = "1";
    }

    jwTaskPageZip(searchForm.value).then(res => {
      searchReturnRes(res)
    })
  }
  // 查课程
  else {
    if (props.isBm) {
      bmJsPageZip(searchForm.value).then(res => {
        searchReturnRes(res)
      })
    }
    if (props.isJw) {
      jwJsPageZip(searchForm.value).then(res => {
        searchReturnRes(res)
      })
    }

  }

}

function searchReturnRes(res) {
  let {totalRow, records, pageNumber, pageSize} = res.data;
  option.value.loading = false;
  option.value.totalRow = totalRow * 1
  option.value.tableData = records
  searchForm.value.pageNumber = pageNumber * 1
  searchForm.value.pageSize = pageSize * 1
}

function downloadFile(row) {
  console.log(row)
  // downloadGet(row.domain + row.uri, row.originName)
  downloadChrome(row.domain + row.uri, row.zipName)
}

function submitDelete(row) {
  let ids = [row.id];
  let names = [row.zipName];

  let content = `正在删除名称为【 ${names.join("、")}】 的数据项，是否确定删除？`

  $modal.confirmWarning(content)
      .then(async () => {
        let res = {};
        if (props.isJw) {
          res = await jwDeleteZipFile({ids: ids.join(",")})
        } else if (props.isBm) {
          res = await bmDeleteZipFile({ids: ids.join(",")})
        } else {
          return;
        }

        if (res.success) {
          $modal.success();
          searchData()
        }

      }).catch(() => $modal.cancel());
}

</script>

<style scoped lang="less">

</style>